package sk.mucha.tracking.classes;

import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.extensions.markup.html.captcha.CaptchaImageResource;

import com.visural.wicket.behavior.beautytips.BeautyTipBehavior;

public class Captcha extends Panel {
	private ChallengeModel challenge = new ChallengeModel();
	private TextField<String> captchaInput;

	public Captcha(String id, FeedbackPanel feedback) {
		super(id);
		setDefaultModel(challenge);
		add(new Image("challenge", new CaptchaImageResource(challenge)));
		captchaInput = new TextField<String>("captchaInput", new Model<String>(null));
		captchaInput.setRequired(true);
		captchaInput.add(new ChallengeValidator(challenge));
		captchaInput.add(new BeautyTipBehavior(getString("enterCaptcha")));
		captchaInput.setLabel(new ResourceModel("label.captcha"));
		captchaInput.add(new AjaxFieldValidator(feedback));
		add(captchaInput);
	}

	@Override
	protected void onBeforeRender() {
		captchaInput.clearInput();
		captchaInput.setModelObject(null);
		challenge.reset();
		super.onBeforeRender();
	}

}
